//app.js

var QQMapWX = require('/utils/qqmap-wx-jssdk.js');
App({
  onLaunch: function () {
   var that = this;

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        if(res.code){
          wx.getSetting({
            success: res => {
              if (!res.authSetting['scope.userInfo']) {
                that.getSession(code)
              }
            }
          })
        }
      }
    })
   
  },
  getCode:function(){
    var that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code;
        if (res.code) {
          wx.getSetting({
            success: res => {
              that.getSession(code)
            }
          })
        }
      }
    })
  },
  getSession:function(code){
    var that = this;
    wx.request({
      url: that.globalData.url +'getOpenIdByCode?code='+code,
      success:function(res){
        // 获取用户信息
        var body = res.data.body
        that.globalData.userId = body.userId;
        that.globalData.openid = body.openid;
        wx.setStorage({
          key: 'openid',
          data: body.openid
        })
        wx.setStorage({
          key: 'userId',
          data: body.userId
        })
            that.getUserInfo()
      
      }
    })
  },
  getUserInfo:function(){
    var that = this;
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          withCredentials: true,
          success: function (res) {
            console.log(res)
            if(res.errMsg == 'getUserInfo:ok'){
              that.globalData.userInfo = res.userInfo;
              wx.setStorage({
                key: 'userInfo',
                data: res.userInfo,
              })
            }
          },
          fail: function (res) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          },
          complete: function (res) { },
        })
      }
    })
  },
  //  获取山西城市列表
  getCity: function (c) {
    var that = this;
    wx.request({
      url: that.globalData.url + 'getArea?parentId=p4',  // p4是获取山西下面的城市列表
      success: function (res) {
        that.getlocate(c, res.data)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  getlocate: function (c, citylist) {
    var that = this;
    // 定位城市 
    wx.getLocation({
      success: function (res) {
        wx.showLoading({
          title: '定位中',
        })
        // 实例化API核心类
        var demo = new QQMapWX({
          key: 'B4YBZ-IS33V-3NWPV-URRKY-S7262-HSBD6' // 必填
        });
        // 调用接口
        demo.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) {
            var selectedCity = {
              city: res.result.address_component.city,
              pro: res.result.address_component.province
            }
            citylist.forEach(function(item){
              if (item.name == selectedCity.city ){

              }
            })
            that.globalData.selectedCity = selectedCity;
            wx.setStorageSync('location', { lat: res.result.location.lat, lng: res.result.location.lng });
            wx.setStorageSync('address_component', res.result.address_component);
            wx.setStorageSync('selectedCity', selectedCity);
            c.setData({
              selectedCity: selectedCity
            })

            //关闭加载提示框 
            wx.hideLoading();
            //执行成功回调
            // f ? f.success ? f.success() : void (0) : void (0);
          },
          fail: function (res) {
            //添加失败提示 
            wx.showModal({
              content: '网络连接失败',
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.reLaunch({
                    url: that.globalData.pages.login,
                  })
                }
                else {
                  wx.navigateBack();
                }
              }
            })
          }
        });
      },
      fail: function () {
        //执行完成回调
        // f ? f.fail ? f.fail() : void (0) : void (0);
      },
      complete: function () {
        //执行完成回调 
        // f ? f.complete ? f.complete() : void (0) : void (0);
      }
    })
  },
  globalData: {
    userInfo: null,
    imgurl:'https://',
    appid:'wx60be6d69fdca464d',
    url: 'https://www.wanxiangrl.com/a/part/' // 'http://211.159.171.242/jeeplus/a/part/',
  }
})